<template>
  <div class="pageOut">
    <div class="pageIn">
      <el-form :inline="true" :model="formSearch" size="small">
        <el-form-item>
          <!-- <el-select
            v-model="formSearch.merchant_id"
            clearable
            filterable
            placeholder="请选择商家"
            style="width:250px;"
          >
            <el-option
              v-for="(item,index) in merchant"
              :key="index"
              :label="item.merchant_name"
              :value="item.id"
            >
              <span style="float: left;overflow:hidden;">{{ item.merchant_name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.merchant_phone }}</span>
            </el-option>
          </el-select>-->
          <el-input v-model="formSearch.cate_name" placeholder="搜索菜品分类名称" clearable></el-input>
        </el-form-item>

        <el-form-item>
          <el-button icon="el-icon-search" @click="search">查询</el-button>
          <el-button
            icon="el-icon-plus"
            type="primary"
            @click="handleAdd"
            v-show="permission.add"
          >新增分类</el-button>
        </el-form-item>
      </el-form>
      <el-table
        border
        ref="table"
        :height="height-170"
        :data="tableData"
        :header-cell-style="{background:'#FAFAFA',color:'#606266'}"
        v-loading="loading"
        element-loading-background="rgba(255, 255, 255, 0.5)"
        style="width: 100%"
      >
        <el-table-column prop="id" width="100" label="序号"></el-table-column>
        <el-table-column prop="cate_name" label="菜品分类名称"></el-table-column>
        <el-table-column prop="merchant_name" :show-overflow-tooltip="tooltip" label="商家名称"></el-table-column>
        <el-table-column prop="cate_content" label="菜品分类内容"></el-table-column>
        <el-table-column prop="checked_image" label="菜品图片" align="center">
          <template slot-scope="scope">
            <div class="img" style="cursor:pointer">
              <viewer>
                <img
                  :src="scope.row.checked_image"
                  alt
                  :onerror="errImg"
                  title="查看高清头像"
                  style="width: 40px;height: 40px;border-radius: 50%"
                />
              </viewer>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间"></el-table-column>

        <el-table-column fixed="right" label="操作" width="240" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              @click="handleEdit(scope.row)"
              v-show="permission.edit"
            >修改</el-button>
            <el-button
              type="danger"
              size="mini"
              @click="handleDel(scope.row.id)"
              v-show="permission.del"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="pageSizes"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>

      <!-- 商家添加和商家编辑 -->
      <el-dialog
        width="550px"
        :title="type==='add'?'添加分类':'修改分类'"
        :visible.sync="visible"
        @close="onCancel('ruleForm')"
        custom-class="custom_drawer"
      >
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          size="mini"
          label-width="120px"
          class="form-drawer__main"
        >
          <el-form-item label="商家" prop="merchant_id" style="margin-bottom:30px;">
            <el-select
              v-model="ruleForm.merchant_id"
              clearable
              filterable
              placeholder="请选择商家"
              style="width:250px;"
            >
              <el-option
                v-for="(item,index) in merchant"
                :key="index"
                :label="item.merchant_name"
                :value="item.id"
              >
                <span style="float: left;overflow:hidden;">{{ item.merchant_name }}</span>
                <span
                  style="float: right; color: #8492a6; font-size: 13px"
                >{{ item.merchant_phone }}</span>
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="菜品分类名称" prop="cate_name" style="margin-bottom:30px;">
            <el-input
              clearable
              v-model="ruleForm.cate_name"
              placeholder="请输入菜品分类名称"
              style="width:250px;"
            ></el-input>
          </el-form-item>
          <el-form-item label="选中图片" prop="checked_image" style="margin-bottom:30px;">
            <UploadImg @getImgSrc="getImgSrc" :imageUrl.sync="ruleForm.checked_image" />
          </el-form-item>

          <el-form-item label="未选中图片" prop="unchecked_image" style="margin-bottom:30px;">
            <UploadImg @getImgSrc="getImgSrc2" :imageUrl.sync="ruleForm.unchecked_image" />
          </el-form-item>

          <el-form-item label="菜品分类内容" prop="cate_content" style="margin-bottom:30px;">
            <el-input
              type="textarea"
              :rows="5"
              clearable
              v-model="ruleForm.cate_content"
              placeholder="请输入菜品分类内容"
            ></el-input>
          </el-form-item>
        </el-form>

        <div slot="footer">
          <el-button @click="onCancel('ruleForm')" size="small">取 消</el-button>
          <el-button
            type="primary"
            size="small"
            @click="onConfirm('ruleForm')"
            :loading="confirmLoading"
          >{{ confirmLoading ? '提交中 ...' : '确 定' }}</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  // getCateList,
  // addCate,
  // editCate,
  // delCate,
  // merchantSelects,
  // MAIN
} from "../../utils/api";
import UploadImg from "../../components/UploadImage";

export default {
  components: {
    UploadImg
  },
  name: "Cate",
  props: ["height"],

  data() {
    return {
      tableData: [],
      tooltip: true, //内容过长被隐藏时显示 tooltip
      loading: true, //显示加载表格
      pageSize: 10, //每页多少条
      currentPage: 1, //当前第几页
      pageSizes: [10, 20, 30, 40, 50], //每页选择条数
      total: 0, //共多少条
      errImg: 'this.src="' + require("../../../static/img/user.png") + '"',
      permission: {
        add: false,
        edit: false,
        del: false
      }, //权限
      ruleForm: {
        unchecked_image: "",
        checked_image: "",
        cate_content: "",
        cate_name: "",
        merchant_id: ""
      }, //搜索
      //编辑弹窗
      visible: false,
      confirmLoading: false,
      type: "",
      currentRow: {}, //当前行数据信息
      formSearch: {
        cate_name: ""
      }, //添加和修改的表单
      rules: {
        cate_name: [
          { required: true, message: "请输入菜品分类名称", trigger: "blur" }
        ],
        cate_content: [
          { required: true, message: "请输入菜品分类内容", trigger: "blur" }
        ],
        merchant_id: [
          { required: true, message: "请选择所属商家", trigger: "change" }
        ],
        checked_image: [
          { required: true, message: "请选择菜品选中的图片", trigger: "change" }
        ],
        checked_image: [
          {
            required: true,
            message: "请选择菜品选中的图片",
            trigger: "change"
          }
        ],
        unchecked_image: [
          {
            required: true,
            message: "请选择菜品未选中的图片",
            trigger: "change"
          }
        ]
      },
      merchant: {}
    };
  },

  mounted() {
    // this.getResource();
  },

  methods: {
    //获取权限资源
    getResource() {
      // console.log(this.$route.name);
      this.loading = true;
      const userinfo = JSON.parse(sessionStorage.getItem('userInfo')); //解密,获取用户登录信息
      let arr = userinfo.menu_id.split(",");
      // console.log(arr)
      // 新增菜品分类 33
      // 修改菜品分类 34
      // 删除菜品分类 35
      if (arr.indexOf("33") > -1) {
        this.permission.add = true;
      }
      if (arr.indexOf("34") > -1) {
        this.permission.edit = true;
      }

      if (arr.indexOf("35") > -1) {
        this.permission.del = true;
      }

      this.getData();
      this.merchantSelects(); //商家下拉列表
    },
    merchantSelects() {
      this.postRequest(merchantSelects, {}, MAIN).then(res => {
        if (res.code == 200) {
          this.merchant = res.data;
        }
      });
    },
    //获取初始数据
    getData() {
      this.loading = true;
      let params = {
        cate_name: this.formSearch.cate_name,
        pageNumber: this.currentPage,
        pageSize: this.pageSize
      };
      this.postRequest(getCateList, params, MAIN).then(res => {
        if (res.code == 200) {
          // console.log(res);
          this.tableData = res.data.list;
          this.total = res.data.count;
          this.loading = false;
          this.$nextTick(() => {
            //避免表格表头样式错乱
            if (this.$refs.table) this.$refs.table.doLayout();
          });
        }
      });
    },
    // 图片绑定
    getImgSrc(v) {
      this.ruleForm.checked_image = v;
    },
    // 图片绑定
    getImgSrc2(v) {
      this.ruleForm.unchecked_image = v;
    },
    //分页
    handleSizeChange(val) {
      //每页 ${val} 条
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      //当前页
      this.currentPage = val;
      this.getData();
    },
    //查询
    search() {
      this.getData();
    },
    //新增
    handleAdd() {
      this.type = "add";
      this.currentRow = {};
      this.visible = true;
    },
    //编辑
    handleEdit(row) {
      this.type = "edit";
      this.currentRow = row;
      this.visible = true;
      this.ruleForm = {
        checked_image: row.checked_image,
        unchecked_image: row.unchecked_image,
        cate_content: row.cate_content,
        cate_name: row.cate_name,
        merchant_id: row.merchant_id
      };
    },
    //删除
    handleDel(id) {
      let parm = {
        id: id
      };
      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.postRequest(delCate, parm, MAIN).then(res => {
          // console.log(res);
          if (res.code == 200) {
            this.$notify({
              title: "删除成功",
              message: res.msg,
              type: "success"
            });
            this.getData();
          }
        });
      }).catch(() => {});
    },

    onConfirm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.confirmLoading = true;
          // setTimeout(() => this.onCancel(formName), 1000);
          if (this.type == "add") {
            this.addCate();
          } else if (this.type == "edit") {
            this.editCate();
          }
        } else {
          return false;
        }
      });
    },
    // 添加
    addCate() {
      let params = {
        checked_image: this.ruleForm.checked_image,
        unchecked_image: this.ruleForm.unchecked_image,
        cate_content: this.ruleForm.cate_content,
        cate_name: this.ruleForm.cate_name,
        merchant_id: this.ruleForm.merchant_id
      };

      this.postRequest(addCate, params, MAIN).then(res => {
        // console.log(res);
        if (res.code == 200) {
          this.$notify({
            title: "添加成功",
            message: res.msg,
            type: "success"
          });
        }
        this.onReset();
        this.getData();
      });
    },
    // 编辑
    editCate() {
      let params = {
        id: this.currentRow.id,
        checked_image: this.ruleForm.checked_image,
        unchecked_image: this.ruleForm.unchecked_image,
        cate_content: this.ruleForm.cate_content,
        cate_name: this.ruleForm.cate_name,
        merchant_id: this.ruleForm.merchant_id
      };

      this.postRequest(editCate, params, MAIN).then(res => {
        // console.log(res);
        if (res.code == 200) {
          this.$notify({
            title: "编辑成功",
            message: res.msg,
            type: "success"
          });
        }
        this.onReset();
        this.getData();
      });
    },
    onCancel(formName) {
      this.$refs[formName].resetFields();
      this.onReset();
    },
    //清空重置表单
    onReset() {
      this.visible = false;
      this.confirmLoading = false;
      this.ruleForm = {
        checked_image: "",
        unchecked_image: "",
        cate_content: "",
        cate_name: "",
        merchant_id: ""
      };
    }
  }
};
</script>

<style scoped>
/deep/ .el-input-group__append,
.el-input-group__prepend {
  background-color: #1890ff;
  color: #fff;
  cursor: pointer;
}
/deep/ .el-input--mini .el-input__inner {
  height: 32px;
  line-height: 32px;
}
</style>
